package io.renren.modules.dangan.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alicp.jetcache.Cache;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.Cached;
import com.alicp.jetcache.anno.CreateCache;
import io.renren.modules.dangan.entity.DaUserFlhEntity;
import io.renren.modules.sys.entity.SysRoleEntity;
import io.renren.modules.sys.service.SysRoleService;
import io.renren.modules.sys.service.SysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.dangan.dao.DaUserLocationDao;
import io.renren.modules.dangan.entity.DaUserLocationEntity;
import io.renren.modules.dangan.service.DaUserLocationService;


@Service("daUserLocationService")
public class DaUserLocationServiceImpl extends ServiceImpl<DaUserLocationDao, DaUserLocationEntity> implements DaUserLocationService {
    @CreateCache(name = "szdCache", cacheType = CacheType.LOCAL, localExpire = 3600, localLimit = 3000)
    private Cache<Integer, List<String>> szdCache;
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Integer userId=Integer.parseInt((String) params.getOrDefault("userId","0"));
        IPage<DaUserLocationEntity> page = this.page(
                new Query<DaUserLocationEntity>().getPage(params),
                new QueryWrapper<DaUserLocationEntity>()
                .eq(userId>0,"user_id",userId)
        );
        Set<String> locationSet=page.getRecords().stream()
                .map(DaUserLocationEntity::getLocation).collect(Collectors.toSet());
        List<Long> roleIds= sysUserRoleService.queryRoleIdList(userId.longValue());
        roleIds.forEach(roleId->{
            SysRoleEntity roleEntity = sysRoleService.findRoleDetailById(roleId);
            if(ObjectUtil.isNotEmpty(roleEntity)){
                locationSet.addAll(roleEntity.getSzds());
            }
        });
        szdCache.put(userId,new ArrayList<>(locationSet));
        return new PageUtils(page);
    }

    @Override
    public boolean saveBatch(int userId, List<String> locations) {
        try {
            Map<String,Object> columnMap=new HashMap<>();
            columnMap.put("user_id",userId);
            baseMapper.deleteByMap(columnMap);
            List<DaUserLocationEntity> daUserLocationEntities=new ArrayList<>();
            Snowflake snowflake= IdUtil.createSnowflake(1, 1);

            locations.forEach(location->{
                DaUserLocationEntity daUserLocationEntity=new DaUserLocationEntity();
                daUserLocationEntity.setUserId(userId);
                daUserLocationEntity.setLocation(location);
                daUserLocationEntity.setRecid(snowflake.nextIdStr());
                daUserLocationEntities.add(daUserLocationEntity);
            });
            if(CollectionUtil.isNotEmpty(daUserLocationEntities)){
                this.saveBatch(daUserLocationEntities);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

}
